/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 皮肤模块
	/// module : skin
	/// </summary>
	public interface SNetSkin : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.skin_wear, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.skin_wear, "Recv->" + data.ToString());
#endif
		                WearRequest _data = (WearRequest)data;
						return CS_wear(session, _data.skinCid);
		            } 
            	},
                { NetMessageId.skin_viewSkin, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.skin_viewSkin, "Recv->" + data.ToString());
#endif
		                ViewSkinRequest _data = (ViewSkinRequest)data;
						return CS_viewSkin(session, _data.skinCid);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.skin_wear, ()=> new WearRequest()},
	        	{NetMessageId.skin_viewSkin, ()=> new ViewSkinRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// 穿着时装   
        /// </summary>
	    protected WearResult CS_wear(ISession session, int skinCid);
		/// <summary>
        /// 查看时装
        /// </summary>
	    protected ViewSkinResult CS_viewSkin(ISession session, int skinCid);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public WearResult SC_wearResult(Task<WearResult> future){return new WearResult(future);}
        public WearResult SC_wearResult(int code){return new WearResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public ViewSkinResult SC_viewSkinResult(Task<ViewSkinResult> future){return new ViewSkinResult(future);}
        public ViewSkinResult SC_viewSkinResult(int code){return new ViewSkinResult(code);}
		/// <summary>
        /// 通知时装更新 type=1获得，type=2更新，type=3删除
        /// </summary>
		public bool SC_notifyUpdateSkin(ISession session, int type, SkinPOD skin){
	        return NetProtocolHelper.SendMessage(session, new NotifyUpdateSkin(type, skin));
	    }
	    public void SC_notifyUpdateSkin_Broadcast(List<ISession> sessions, int type, SkinPOD skin){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyUpdateSkin(type, skin));
	    }
#endregion

#region Messages
	public class WearResult : MessageResult {
	    public WearResult(Task<WearResult> future): base(future) { }
		public WearResult(int code): base(code) { }
		public new WearResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<WearResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.skin_wearResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.skin_wearResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Skin.SC_wearResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class ViewSkinResult : MessageResult {
	    public ViewSkinResult(Task<ViewSkinResult> future): base(future) { }
		public ViewSkinResult(int code): base(code) { }
		public new ViewSkinResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<ViewSkinResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.skin_viewSkinResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.skin_viewSkinResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Skin.SC_viewSkinResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class NotifyUpdateSkin : MessageNotify {
		private int type;
		private SkinPOD skin;
		public NotifyUpdateSkin(int type, SkinPOD skin) {
		    this.type = type;
		    this.skin = skin;
		}
		public override int GetMessageId() {return NetMessageId.skin_notifyUpdateSkin;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.skin_notifyUpdateSkin;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(type);
			_out.Write_pod(skin);
			return si_p;
		}
		public override string ToString() {
			return "Skin.SC_notifyUpdateSkin { "
			+ "type="
			+ type
			+ ", skin="
			+ skin
			+" }";
		}
	}
	private class WearRequest : IReceiveMessage
    {
		internal int skinCid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            skinCid = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.skin_wear; }
        public override string ToString()
        {
            return "Skin.wear"+ ", skinCid="+ skinCid;
        }
    }
	private class ViewSkinRequest : IReceiveMessage
    {
		internal int skinCid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            skinCid = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.skin_viewSkin; }
        public override string ToString()
        {
            return "Skin.viewSkin"+ ", skinCid="+ skinCid;
        }
    }
	}
#endregion
}